<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Auto Width Horizontal Menu Bar</title>
<script src="../../widgets/menubar/SpryMenuBar.js" type="text/javascript"></script>
<link href="../../css/samples.css" rel="stylesheet" type="text/css" />
<link href="../../widgets/menubar/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.clearAll {
	clear: both;
}
.sidebar {
	/*width: 200px;*/
}

ul.MenuBarHorizontal#Step1MenuBar li {
	width: auto;
}
ul.MenuBarHorizontal#Step1MenuBar ul {
	width: auto;
}

ul.MenuBarHorizontal#Step1MenuBar ul li {
	display: block;
	float: none;
	width: auto;
	white-space: nowrap;
}

ul.MenuBarHorizontal#Step1MenuBar a.MenuBarItemSubmenu {
	padding: 0.5em 2em 0.5em 0.75em;
}

ul.MenuBarHorizontal#Step2MenuBar li {
	width: auto;
}
ul.MenuBarHorizontal#Step2MenuBar ul {
	width: auto;
}

ul.MenuBarHorizontal#Step2MenuBar ul li {
	display: block;
	float: none;
	width: auto;
	white-space: nowrap;
}

ul.MenuBarHorizontal#Step2MenuBar a.MenuBarItemSubmenu {
	padding: 0.5em 2em 0.5em 0.75em;
}

ul.MenuBarHorizontal#Step3MenuBar li {
	width: auto;
}
ul.MenuBarHorizontal#Step3MenuBar ul {
	width: auto;
}

ul.MenuBarHorizontal#Step3MenuBar ul li {
	display: block;
	float: none;
	width: auto;
	white-space: nowrap;
	border-bottom: solid 1px #EEE;
}

ul.MenuBarHorizontal#Step3MenuBar a.MenuBarItemSubmenu {
	padding: 0.5em 2em 0.5em 0.75em;
}
-->
</style>
</head>
<body>
<h3>Auto Width Horizontal Menu Bar</h3>
<p>This page shows you how to modify the default styles for a Horizontal Menu Bar, so that the menu items within the Menu Bar and its sub-menus are just wide enough to accomodate the longest menu item string. This sounds easy, but you have to employ a couple of workarounds for specific <a href="#iebug">IE</a> and <a href="#mozillabug">Mozilla/FireFox</a> layout bugs. These layout bugs do not occur if you specify widths on menu items as is done in the default Menu Bar stylesheet.</p>
<p>Below, you'll notice that each CSS code sample starts off by including the default menu bar style sheet, and then declares a &lt;style&gt; block immediately after it, to override some of the default style properties set in the style sheet. The code is presented this way to show the minimal set of changes  that are necessary to accomplish the task. If you want these changes to affect more than one page, then you may want to integrate these changes directly into the style sheet.</p>
<p>This is what the standard Horizontal Menu Bar looks like:</p>
<div class="liveSample">
	<ul id="DefaultStyleMenuBar1" class="MenuBarHorizontal">
		<li><a class="MenuBarItemSubmenu" href="#">Item 1</a>
				<ul>
					<li><a href="#">Item 1.1</a></li>
					<li><a href="#">Item 1.2</a></li>
					<li><a href="#">Item 1.3</a></li>
					<li><a href="#">Item 1.4</a></li>
				</ul>
		</li>
		<li><a href="#">Item 2</a></li>
		<li><a class="MenuBarItemSubmenu" href="#">Item 3</a>
				<ul>
					<li><a class="MenuBarItemSubmenu" href="#">Item 3.1</a>
							<ul>
								<li><a href="#">Item 3.1.1</a></li>
								<li><a href="#">Item 3.1.2</a></li>
								<li><a href="#">Item 3.1.3</a></li>
							</ul>
					</li>
					<li><a href="#">Item 3.2</a></li>
					<li><a href="#">Item 3.3</a></li>
					<li><a href="#">Item 3.4</a></li>
				</ul>
		</li>
		<li><a class="MenuBarItemSubmenu" href="#">Item4</a>
				<ul>
					<li><a class="MenuBarItemSubmenu" href="#">Item 4.1</a>
							<ul>
								<li><a href="#">Item 4.1.1</a></li>
								<li><a href="#">Item 4.1.2</a></li>
								<li><a href="#">Item 4.1.3</a></li>
							</ul>
					</li>
					<li><a href="#">Item 4.2</a></li>
					<li><a href="#">Item 4.3</a></li>
					<li><a href="#">Item 4.4</a></li>
				</ul>
		</li>
	</ul>
	<div class="clearAll">&nbsp;</div>
	<script type="text/javascript">
<!--
var DefaultStyleMenuBar1 = new Spry.Widget.MenuBar("DefaultStyleMenuBar1", {imgDown:"../../widgets/menubar/SpryMenuBarDownHover.gif", imgRight:"../../widgets/menubar/SpryMenuBarRightHover.gif"});
//-->
	</script>
</div>
<p>But when you start using long strings in both the menu bar and sub menus, it can look like this:</p>
<div class="liveSample">
	<ul id="DefaultStyleMenuBar2" class="MenuBarHorizontal">
		<li><a class="MenuBarItemSubmenu" href="#">Item 1</a>
			<ul>
				<li><a href="#">Item 1.1</a></li>
				<li><a href="#">Item 1.2</a></li>
				<li><a href="#">Item 1.3</a></li>
				<li><a href="#">This is a long menu item string!</a></li>
			</ul>
		</li>
		<li><a href="#">Item 2</a></li>
		<li><a class="MenuBarItemSubmenu" href="#">Item 3 - With a long menu item string!</a>
			<ul>
				<li><a class="MenuBarItemSubmenu" href="#">Item 3.1 - With a long menu item string!</a>
					<ul>
						<li><a href="#">Item 3.1.1</a></li>
						<li><a href="#">Item 3.1.2</a></li>
						<li><a href="#">This is a long menu item string!</a></li>
					</ul>
				</li>
				<li><a href="#">Item 3.2</a></li>
				<li><a href="#">Item 3.3</a></li>
				<li><a href="#">ThisIsALongMenuItemString!</a></li>
			</ul>
		</li>
		<li><a class="MenuBarItemSubmenu" href="#">Item4-WithAStringThatDoesNotWrap</a>
			<ul>
				<li><a class="MenuBarItemSubmenu" href="#">Item 4.1</a>
					<ul>
						<li><a href="#">Item 4.1.1</a></li>
						<li><a href="#">Item 4.1.2</a></li>
						<li><a href="#">WithAStringThatDoesNotWrap</a></li>
					</ul>
				</li>
				<li><a href="#">Item 4.2</a></li>
				<li><a href="#">Item 4.3</a></li>
				<li><a href="#">WithAStringThatDoesNotWrap</a></li>
			</ul>
		</li>
	</ul>
	<div class="clearAll">&nbsp;</div>
<script type="text/javascript">
<!--
var DefaultStyleMenuBar2 = new Spry.Widget.MenuBar("DefaultStyleMenuBar2", {imgDown:"../../widgets/menubar/SpryMenuBarDownHover.gif", imgRight:"../../widgets/menubar/SpryMenuBarRightHover.gif"});
//-->
</script>
</div>
<p>The default CSS for menu items in both the menu bar and its sub menus have a width property specified on them that give them a default with of 8.2em, which gives all of the menus a uniform look when the text fits neatly within that width, but it forces any text that is wider than that to wrap or hang out of the menu.</p>
<p>Removing the specified widths:</p>
<pre>
&lt;link href=&quot;../../widgets/menubar/SpryMenuBarHorizontal.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
&lt;style type=&quot;text/css&quot;&gt;
&lt;!--
/* We want to remove any specified width on the menu bar
 * menu items.
 */

ul.MenuBarHorizontal li {
	width: auto;
}

/* Remove any widths on sub menus. */

ul.MenuBarHorizontal ul {
	width: auto;
}

/* We want the menu items in our sub menus to
 * fill up the entire width of the sub menu, so
 * make sure it is display:block and not floated.
 * Also remove any specified width from the default
 * style sheet and turn on &quot;nowrap&quot;.
 */

ul.MenuBarHorizontal ul li {
	display: block;
	float: none;
	width: auto;
	white-space: nowrap;
}

/* Now that our menus auto size horizontally, we need to
 * make sure that we have some space for any sub menu indicators
 * so they don't overlap with the text in the menu item.
 */

ul.MenuBarHorizontal a.MenuBarItemSubmenu {
	padding: 0.5em 2em 0.5em 0.75em;
}

--&gt;
&lt;/style&gt;
</pre>
<p>makes things a bit better:</p>
<div class="liveSample">
	<div class="sidebar">
	<ul id="Step1MenuBar" class="MenuBarHorizontal">
		<li><a class="MenuBarItemSubmenu" href="#">Item 1</a>
			<ul>
				<li><a href="#">Item 1.1</a></li>
				<li><a href="#">Item 1.2</a></li>
				<li><a href="#">Item 1.3</a></li>
				<li><a href="#">This is a long menu item string!</a></li>
			</ul>
		</li>
		<li><a href="#">Item 2</a></li>
		<li><a class="MenuBarItemSubmenu" href="#">Item 3 - With a long menu item string!</a>
			<ul>
				<li><a class="MenuBarItemSubmenu" href="#">Item 3.1 - With a long menu item string!</a>
					<ul>
						<li><a href="#">Item 3.1.1</a></li>
						<li><a href="#">Item 3.1.2</a></li>
						<li><a href="#">This is a long menu item string!</a></li>
					</ul>
				</li>
				<li><a href="#">Item 3.2</a></li>
				<li><a href="#">Item 3.3</a></li>
				<li><a href="#">ThisIsALongMenuItemString!</a></li>
			</ul>
		</li>
		<li><a class="MenuBarItemSubmenu" href="#">Item4-WithAStringThatDoesNotWrap</a>
			<ul>
				<li><a class="MenuBarItemSubmenu" href="#">Item 4.1</a>
					<ul>
						<li><a href="#">Item 4.1.1</a></li>
						<li><a href="#">Item 4.1.2</a></li>
						<li><a href="#">WithAStringThatDoesNotWrap</a></li>
					</ul>
				</li>
				<li><a href="#">Item 4.2</a></li>
				<li><a href="#">Item 4.3</a></li>
				<li><a href="#">WithAStringThatDoesNotWrap</a></li>
			</ul>
		</li>
	</ul>
	<div class="clearAll">&nbsp;</div>
	</div>
<script type="text/javascript">
<!--
var Step1MenuBar = new Spry.Widget.MenuBar("Step1MenuBar", {imgDown:"../../widgets/menubar/SpryMenuBarDownHover.gif", imgRight:"../../widgets/menubar/SpryMenuBarRightHover.gif"});
//-->
</script>
</div>
<p>But, now we have a couple of problems:</p>
<ul>
	<li>In IE the sub menus now have white gaps between the menu items!</li>
	<li>In Mozilla/FireFox 1.5, when the text within a menu bar menu item is wider than any of the text in its sub menu, the &lt;li&gt; elements of the sub menu render with collapsed widths so the menu items no longer span the entire width of the sub menu</li>
</ul>
<p><a name="iebug" id="iebug"></a>The IE bug that is very similar to the &quot;<a href="http://www.adobe.com/cfusion/communityengine/index.cfm?event=findByTextId&amp;productid=1&amp;textid=EXTRA_WHITESPACE_IN_LIST_LINKS&amp;loc=en_US">Extra Whitespace in List Links bug</a>&quot;. But the suggested workarounds don't seem to work. For example, removing all the extraneous white space within the menubar's lists still yields gaps:</p>
<div class="liveSample">
	<ul id="Step2MenuBar" class="MenuBarHorizontal"><li><a class="MenuBarItemSubmenu" href="#">Item 1</a><ul><li><a href="#">Item 1.1</a></li><li><a href="#">Item 1.2</a></li><li><a href="#">Item 1.3</a></li><li><a href="#">This is a long menu item string!</a></li></ul></li><li><a href="#">Item 2</a></li><li><a class="MenuBarItemSubmenu" href="#">Item 3 - With a long menu item string!</a><ul><li><a class="MenuBarItemSubmenu" href="#">Item 3.1 - With a long menu item string!</a><ul><li><a href="#">Item 3.1.1</a></li><li><a href="#">Item 3.1.2</a></li><li><a href="#">This is a long menu item string!</a></li></ul></li><li><a href="#">Item 3.2</a></li><li><a href="#">Item 3.3</a></li><li><a href="#">ThisIsALongMenuItemString!</a></li></ul></li><li><a class="MenuBarItemSubmenu" href="#">Item4-WithAStringThatDoesNotWrap</a><ul><li><a class="MenuBarItemSubmenu" href="#">Item 4.1</a><ul><li><a href="#">Item 4.1.1</a></li><li><a href="#">Item 4.1.2</a></li><li><a href="#">WithAStringThatDoesNotWrap</a></li></ul></li><li><a href="#">Item 4.2</a></li><li><a href="#">Item 4.3</a></li><li><a href="#">WithAStringThatDoesNotWrap</a></li></ul></li></ul>
	<div class="clearAll">&nbsp;</div>
	<script type="text/javascript">
<!--
var Step2MenuBar = new Spry.Widget.MenuBar("Step2MenuBar", {imgDown:"../../widgets/menubar/SpryMenuBarDownHover.gif", imgRight:"../../widgets/menubar/SpryMenuBarRightHover.gif"});
//-->
	</script>
</div>
<p>Also, trying to use the hack of display:inline-block followed by display:block to turn on hasLayout causes the layout to totally break-down. The only workaround we've been able to find for this problem is to place a border-bottom on the element, so as a workaround, we will add a border-bottom to our menu item &lt;li&gt; elements that is the same color as the background of the menu, so it blends in with the background:</p>
<pre>
&lt;link href=&quot;../../widgets/menubar/SpryMenuBarHorizontal.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
&lt;style type=&quot;text/css&quot;&gt;
&lt;!--
/* We want to remove any specified width on the menu bar
 * menu items.
 */

ul.MenuBarHorizontal li {
	width: auto;
}

/* Remove any widths on sub menus. */

ul.MenuBarHorizontal ul {
	width: auto;
}

/* We want the menu items in our sub menus to
 * fill up the entire width of the sub menu, so
 * make sure it is display:block and not floated.
 * Also remove any specified width from the default
 * style sheet and turn on &quot;nowrap&quot;.
 */

ul.MenuBarHorizontal ul li {
	display: block;
	float: none;
	width: auto;
	white-space: nowrap;
	border-bottom: solid 1px #EEE;
}

/* Now that our menus auto size horizontally, we need to
 * make sure that we have some space for any sub menu indicators
 * so they don't overlap with the text in the menu item.
 */

ul.MenuBarHorizontal a.MenuBarItemSubmenu {
	padding: 0.5em 2em 0.5em 0.75em;
}

--&gt;
&lt;/style&gt;
</pre>
<p>makes things a bit better in IE:</p>
<div class="liveSample">
	<div class="sidebar">
	<ul id="Step3MenuBar" class="MenuBarHorizontal">
		<li><a class="MenuBarItemSubmenu" href="#">Item 1-WithAStringThatDoesNotWrap</a>
			<ul>
				<li><a href="#">Item 1.1</a></li>
				<li><a href="#">Item 1.2</a></li>
				<li><a href="#">Item 1.3</a></li>
				<li><a href="#">This is a long menu item string!</a></li>
			</ul>
		</li>
		<li><a href="#">Item 2</a></li>
		<li><a class="MenuBarItemSubmenu" href="#">Item 3 - With a long menu item string!</a>
			<ul>
				<li><a class="MenuBarItemSubmenu" href="#">Item 3.1 - With a long menu item string!</a>
					<ul>
						<li><a href="#">Item 3.1.1</a></li>
						<li><a href="#">Item 3.1.2</a></li>
						<li><a href="#">This is a long menu item string!</a></li>
					</ul>
				</li>
				<li><a href="#">Item 3.2</a></li>
				<li><a href="#">Item 3.3</a></li>
				<li><a href="#">ThisIsALongMenuItemString!</a></li>
			</ul>
		</li>
		<li><a class="MenuBarItemSubmenu" href="#">Item4-WithAStringThatDoesNotWrap</a>
			<ul>
				<li><a class="MenuBarItemSubmenu" href="#">Item 4.1</a>
					<ul>
						<li><a href="#">Item 4.1.1</a></li>
						<li><a href="#">Item 4.1.2</a></li>
						<li><a href="#">WithAStringThatDoesNotWrap</a></li>
					</ul>
				</li>
				<li><a href="#">Item 4.2</a></li>
				<li><a href="#">Item 4.3</a></li>
				<li><a href="#">WithAStringThatDoesNotWrap&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></li>
			</ul>
		</li>
	</ul>
	<div class="clearAll">&nbsp;</div>
	</div>
<script type="text/javascript">
<!--
var Step3MenuBar = new Spry.Widget.MenuBar("Step3MenuBar", {imgDown:"../../widgets/menubar/SpryMenuBarDownHover.gif", imgRight:"../../widgets/menubar/SpryMenuBarRightHover.gif"});
//-->
</script>
</div>
<p><a name="mozillabug" id="mozillabug"></a>But we still have the problem in Mozilla/FireFox. When the text within a menu bar menu item is wider than any of the text in its sub menu, the &lt;li&gt; elements of the sub menu render with collapsed widths so the menu items no longer span the entire width of the sub menu. This bug seems to be triggered by the fact that the menu items in the menu bar are float:left. You will notice that The first and fourth menus in the example above exhibit this problem, but none of the sub menus of sub menus exhibit this problem.</p>
<p>The only way we've been able to get around this bug is by either specifying a width on the affected sub menu's &lt;ul&gt;, or by padding out the longest string in the sub menu with non-breaking spaces till its width exceeded that of the menu bar menu item.</p>
<p>&nbsp;</p>
</body>
</html>
